import 'dart:math';

import 'package:flutter/material.dart';
import 'package:t2app/src/Config.dart';
import 'package:t2app/src/common/Tool.dart';

import 'YesServer_Chat_View.dart';

class YesServer_Index extends StatelessWidget{

  /**
   * 获按钮的文字显示
   */
  _getBtnText(int index) {
    String text;
    switch(index) {
      case 0:  text = '聊天室'; break;
      default: text = '占位...';
    }
    return text;
  }

  /**
   * 获取按钮跳转页
   */
  _getBtnNav(int index, BuildContext context) {
    Widget? view;
    switch(index) {
      case 0:  view = new YesServer_Chat_View(); break;
    }
    return view;
  }


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
        appBar: Config.APP_BAR(),
        body: GridView.count(
          crossAxisCount: 2,
          children: List.generate(10, (index) {
            return GestureDetector(
              onTap: () {
                Widget? view =_getBtnNav(index, context);
                if(view == null) {
                  Tool.EasyLoading_Toast("没有内容哦^_^");
                } else {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => view,
                    ),
                  );
                }
              },
              child: Container(
                margin: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: Colors.grey[300],
                  borderRadius: BorderRadius.circular(8),
                  image: DecorationImage(
                    image: AssetImage(
                      getRandomImagePath(),
                    ),
                    fit: BoxFit.cover,
                  ),
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Container(
                      width: double.infinity,
                      alignment: Alignment.center,
                      color: Config.THEME.primaryColorLight,
                      child: Text(
                        _getBtnText(index), // 为每个按钮设置不同的文字
                        style: TextStyle(
                            color: Colors.white, // 设置文本颜色为白色
                            fontSize: 24
                        ),
                      ),
                    )
                  ],
                ),
              ),
            );
          }),
        ));
  }

  String getRandomImagePath() {
    final random = Random();
    final randomNumber = random.nextInt(5) + 1;
    return 'assets/images/btnbak/100$randomNumber.png';
  }
}
